package net.crowdloud.client.user;

import net.crowdloud.shared.entities.User;
import net.crowdloud.shared.utilities.FormField;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.TextArea;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;



/** 
 * Collect user information to build a database with their skills and interests
 both to contribute or receive from a certain community of other users.
TODO: Add the Zip code on the user information.
 * 
 * @TODO
 * @MainFeature 
 */
public class UserInformation extends VerticalPanel {

	public static final TextBox name = new TextBox();

	public static final TextBox contact = new TextBox();

	public static final TextArea groups = new TextArea();

	public UserInformation(User user) {

		this.setBorderWidth(1);

		VerticalPanel vp = new VerticalPanel();
		vp.setSize("100%", "100%");

		vp.setSpacing(30);

		vp.add(new ButtonUserPage(user));

		name.setWidth("500px");
		name.setValue(user.getName());
		vp.add(new FormField("Name", name));

		contact.setWidth("500px");
		contact.setValue(user.getContact());
		vp.add(new FormField("Email", contact));

		groups.setWidth("500px");
		groups.setValue(user.getGroups());
		vp.add(new FormField("Groups", groups));

		vp.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);

		HorizontalPanel hp = new HorizontalPanel();

		hp.setWidth("300px");

		hp.add(buttonSaveUser(user));

		vp.add(hp);

		this.add(vp);
	}

	private Button buttonSaveUser(final User user) {

		Button button = new Button("Save", new ClickHandler() {

			@Override
			public void onClick(ClickEvent event) {

				user.setName(name.getValue());
				user.setContact(contact.getValue());
				user.setGroups(groups.getValue());

				new UpdateUser(user);
			}
		});

		return button;
	}

}
